const app = getApp();

// 封装微信的 request
function http_request(url, data = {}, method = "GET", customHeaders = {}) {
  return new Promise((resolve, reject) => {
    wx.showLoading({ title: "加载中..." });

    // 默认请求头
    let headers = {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      ...customHeaders,  // 允许用户自定义额外的请求头
    };

    wx.request({
      url,
      data,
      method,
      header: headers,
      timeout: 10000, 
      success: res => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(res);
        }
      },
      fail: reject,
      complete: wx.hideLoading
    });
  });
}

module.exports = { http_request };
